//
//  HTTPHelper+Service.m
//  SalonManage
//
//  Created by wulongmin on 17/7/16.
//  Copyright © 2017年 lmtt. All rights reserved.
//

#import "HTTPHelper+Service.h"

#import "ServiceInfo.h"

@implementation HTTPHelper (Service)

- (NSURLSessionDataTask *)getServiceInfoWithSerKind:(NSInteger)serkind
                                            custNum:(NSString *)cust_num
                                         balanceOdd:(NSString *)balance_odd
                                           custKind:(NSInteger)cust_kind
                                            success:(void(^)(NSMutableArray <ServiceInfo *> *))success {
    
    NSString *url = [API_SERVICE stringByAppendingString:@"list"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"serkind"] = @(serkind);
    params[@"cust_num"] = cust_num;
    params[@"balance_odd"] = balance_odd;
    params[@"cust_kind"] = @(cust_kind);
    
    return [self get:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            
            NSMutableArray *serviceInfoList = [ServiceInfo mj_objectArrayWithKeyValuesArray:result];
            
            success(serviceInfoList);
        }else {
            
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)addSericeInfoWithDataKind:(NSInteger)datakind
                                            custNum:(NSString *)cust_num
                                           ser_kind:(NSInteger)ser_kind
                                                tel:(NSString *)tel
                                            empName:(NSString *)emp_name
                                            success:(void(^)())success {
    
    NSString *url = [API_SERVICE stringByAppendingString:@"add"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"datakind"] = @(datakind);
    if (datakind == 1) {
        params[@"ass_cust_num"] = cust_num;
    }else if (datakind == 3) {
        params[@"hair_cust_num"] = cust_num;
    }else if (datakind == 4) {
        params[@"tech_cust_num"] = cust_num;
    }else {
        [MBProgressHUD showText:@"datakind只能是1，3，4" onView:nil];
        return nil;
    }
    params[@"ser_kind"] = @(ser_kind);
    params[@"tel"] = tel;
    params[@"emp_name"] = emp_name;
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            success();
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)updateServieInfoWithDatakind:(NSInteger)datakind
                                           serviceInfo:(ServiceInfo *)serviceInfo
                                               success:(void(^)())success {
    
    if (datakind == 1 || datakind == 3 || datakind == 4) {
        [MBProgressHUD showText:@"datakind数值有误" onView:nil];
        return nil;
    }
    
    NSString *url = [API_SERVICE stringByAppendingString:@"update"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"datakind"] = @(datakind);
    params[@"ser_kind"] = @(serviceInfo.ser_kind);
    params[@"balance_odd"] = serviceInfo.balance_odd;
    params[@"tel"] = serviceInfo.tel;
    params[@"emp_name"] = serviceInfo.emp_name;
    params[@"ass_cust_num"] = serviceInfo.ass_cust_num;
    params[@"hair_cust_num"] = serviceInfo.hair_cust_num;
    params[@"tech_cust_num"] = serviceInfo.tech_cust_num;
    params[@"techkind"] = @(serviceInfo.techkind);
    params[@"serkind"] = @(serviceInfo.serkind);
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            success();
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
